from typing import List
from core import Config
import redis

class KeyValueStorage:  
    """  
    同步版本的键值存储类  
    """  
    def __init__(self, host=Config.REDIS_HOST,   
                 port=Config.REDIS_PORT, db=Config.REDIS_DB,   
                 password=Config.REDIS_PASSWORD, prefix=Config.DATA_STORE_PREFIX):    
  
        self.prefix = prefix    
        self.redis = redis.StrictRedis(host=host, port=port, db=db, password=password)  # 创建同步 Redis 客户端实例  
  
    def set(self, key, value):    
        full_key = self.prefix + key    
        self.redis.set(full_key, value)    
    
    def get(self, key):    
        full_key = self.prefix + key    
        value = self.redis.get(full_key)    
        if value is not None:    
            return value.decode('utf-8')    
        return None    
    
    def delete(self, key):    
        full_key = self.prefix + key    
        self.redis.delete(full_key)    
    
    def exists(self, key):    
        full_key = self.prefix + key    
        return self.redis.exists(full_key)    
    
    def get_all_keys(self):    
        keys = self.redis.keys(self.prefix + '*')    
        return [key.decode('utf-8') for key in keys]  
    
    def close(self):  
        # 对于同步版本的 Redis 客户端，通常不需要显式关闭连接  
        # 但如果需要，可以使用下面的代码来关闭连接池中的连接  
        # 如果使用了默认的连接池设置，则可以省略此部分  
        # self.redis.connection_pool.disconnect()  
        pass


from aioredis import from_url,Redis

class AsyncKeyValueStorage:
    """
    使用示例:
        async def main():  
            storage = AsyncKeyValueStorage(prefix='user123:')  
            await storage.initialize()  
            
            await storage.set('name', 'John Doe')  
            await storage.set('age', '30')  
            print(await storage.get('name'))  # 输出: John Doe  
            print(await storage.get('age'))   # 输出: 30  
            print(await storage.exists('name'))  # 输出: True  
            print(await storage.get_all_keys())  # 输出带有前缀的所有键  
            await storage.delete('age')  
            print(await storage.get('age'))  # 输出: None  
            
            await storage.close()  
    
        # 运行异步主函数  
        asyncio.run(main())
    """
    def __init__(self, host=Config.REDIS_HOST, 
                 port=Config.REDIS_PORT, db=Config.REDIS_DB, 
                 password=Config.REDIS_PASSWORD, prefix=Config.DATA_STORE_PREFIX):  

        self.prefix = prefix  
        self.redis: Redis = None  
        self._create_redis = lambda: from_url(  
            f"redis://:{password}@{host}:{port}/{db}",  
            minsize=5, maxsize=10,
        )

    async def initialize(self):  
        self.redis = await self._create_redis()  
  
    async def close(self):
        await self.redis.close()

    async def set(self, key, value):  
        full_key = self.prefix + key  
        await self.redis.set(full_key, value)  
  
    async def get(self, key):  
        full_key = self.prefix + key  
        value: bytes = await self.redis.get(full_key)  
        if value is not None:  
            return value.decode('utf-8')  
        return None  
  
    async def delete(self, key):  
        full_key = self.prefix + key  
        await self.redis.delete(full_key)  
  
    async def exists(self, key):  
        full_key = self.prefix + key  
        return await self.redis.exists(full_key)  
  
    async def get_all_keys(self):  
        keys: List[bytes] = await self.redis.keys(self.prefix + '*')  
        return [key.decode('utf-8') for key in keys]